博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts的增删改查
阅读量:5055 次
发布时间:2019-06-12

本文共 9756 字,大约阅读时间需要 32 分钟。

1、导入相关的pom依赖(struts、自定义标签库的依赖)

    
    
commons-fileupload
    
commons-fileupload
    
1.3.1
    
    
jstl
jstl
1.2
taglibs
standard
1.1.2
org.apache.tomcat
tomcat-jsp-api
8.0.47

 

2、分页的tag类导入、z.tld、完成web.xml的配置

PageTag
package com.tag;import java.util.Map;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyTagSupport;import com.util.PageBean;public class PageTag extends BodyTagSupport {    private static final long serialVersionUID = 8003507841172246203L;    private PageBean pageBean;    public PageTag() {        super();    }    public PageBean getPageBean() {        return pageBean;    }    public void setPageBean(PageBean pageBean) {        this.pageBean = pageBean;    }    @Override    public int doStartTag() throws JspException {        try {            JspWriter out = pageContext.getOut();            out.println(this.toHtml());            return SKIP_BODY;        } catch (Exception e) {            throw new RuntimeException(e);        }    }    private String toHtml() {        StringBuffer bf = new StringBuffer();        if (null == pageBean || !pageBean.isPagination()) {            return bf.toString();        }        // 1. 生成表单        bf.append("
"); bf.append("
"); for (Map.Entry
entry : pageBean.getParameterMap().entrySet()) { String name = entry.getKey(); String[] values = entry.getValue(); if ("page".equals(name)) { continue; } for (String value : values) { bf.append("
"); } } bf.append(""); // 2. 生成按钮 bf.append("
每页" + pageBean.getRows() + "条,共" + pageBean.getTotal() + "条,当前第" + pageBean.getPage() + "页,共" + pageBean.getMaxPage() + "页  
首  页  
上一页  
下一页  
尾  页  
  
GO
"); // 3. 动态生成js bf.append("
"); bf.append(""); bf.append(""); bf.append(""); bf.append(""); bf.append(""); return bf.toString(); }}

z.tld

1.0
1.2
Simple Tags
/zking
page
com.tag.PageTag
empty
pageBean
true
true

web.xml

Archetype Created Web Application
encodingFiter
com.util.EncodingFiter
encodingFiter
/*
struts2
org.apache.struts2.factory.StrutsActionProxy
struts2
*.action

 

3、dao层去访问数据

创建与数据库相对应的实体类

 

Clazz
package com.entity;public class Clazz {    private int cid;    private String cname;    private String cteacher;    private String pic;    public int getCid() {        return cid;    }    public void setCid(int cid) {        this.cid = cid;    }    public String getCname() {        return cname;    }    public void setCname(String cname) {        this.cname = cname;    }    public String getCteacher() {        return cteacher;    }    public void setCteacher(String cteacher) {        this.cteacher = cteacher;    }    public String getPic() {        return pic;    }    public void setPic(String pic) {        this.pic = pic;    }    @Override    public String toString() {        return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";    }    }

ClazzDao

package com.dao;import java.sql.SQLException;import java.util.List;import com.entity.Clazz;import com.util.BaseDao;import com.util.PageBean;import com.util.StringUtils;public class ClazzDao extends BaseDao
{ /** * 查詢 * @param clazz * @param pageBean * @return * @throws Exception */ public List
list(Clazz clazz,PageBean pageBean) throws Exception{ String sql="select * from t_struts_class where true "; String cname=clazz.getCname(); int cid=clazz.getCid(); if(cid!=0) { sql+=" and cid="+cid; } if(StringUtils.isNotBlank(cname)) { sql+=" and cname like '%"+cname+"%'"; } return super.executeQuery(sql, Clazz.class, pageBean); } /** * 新增 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int add(Clazz clazz) throws NoSuchFieldException, Exception { String sql="insert into t_struts_class values(?,?,?,?)"; return super.executeUpdate(sql, new String [] {"cid","cname","cteacher","pic"}, clazz); } /** * 删除 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int del(Clazz clazz) throws NoSuchFieldException, Exception { String sql="delete from t_struts_class where cid=? "; return super.executeUpdate(sql, new String [] {"cid"}, clazz); } /** * 修改 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int edit(Clazz clazz) throws NoSuchFieldException, Exception { String sql=" update t_struts_class set cname= ? , chteacher=? ,pic=? where cid=? "; return super.executeUpdate(sql, new String [] {"cname","cteacher","pic","cid"}, clazz); }}

4、web层去调用dao层给前台返回数据

ClazzAction

package com.web;import java.util.List;import com.dao.ClazzDao;import com.entity.Clazz;import com.opensymphony.xwork2.ModelDriven;import com.util.PageBean;public class ClazzAction extends BaseAction implements ModelDriven
{ private Clazz clz=new Clazz(); private ClazzDao clazzDao=new ClazzDao(); public String list() { PageBean pageBean=new PageBean(); pageBean.setRequest(request); try { List
list=this.clazzDao.list(clz, pageBean); request.setAttribute("clzList", list); request.setAttribute("pageBean", pageBean); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "list"; } /** * 跳转编辑页面(新增修改页面) */ public String preSave() { if(clz.getCid()!=0) { try { this.result=this.clazzDao.list(clz, null).get(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "preSave"; } public String add() { try { this.code=this.clazzDao.add(clz); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String edie() { try { this.clazzDao.edit(clz); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String del() { try { this.clazzDao.del(clz); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } @Override public Clazz getModel() { // TODO Auto-generated method stub return clz; } }

 

5、在struts_sy.xml进行配置

/clzList.jsp
/clzEdit.jsp
/clz_List

 

6、写jsp页面

clzList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@page isELIgnored="false"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@taglib prefix="z" uri="/zking"%>
Insert title here

小说目录

班级名称:
增加
编号 班级名称 教员 班级图片 操作
${c.cid } ${c.cname } ${c.cteacher } ${c.pic } 修改  删除 图片上传

 

ClzEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
班级编辑界面
<%--
--%> cid:
cname:
cteacher:

页面效果

 

转载于:https://www.cnblogs.com/huxiaocong/p/11261552.html

你可能感兴趣的文章
返回代码hdu 2054 A==B?
查看>>
iOS 8 地图
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>